// pages/register/register.js
var _check = require('../../utils/check.js');
var _tip = require('../../utils/tip.js');
var _swit = require('../../utils/swit.js');
Page({
  data: {
    logoUrl: '../../images/register_logo.png',
    subLogoUrl: '../../images/register_ozner.png',
    pwdHide: '../../images/register_eyes_hide.png',
    pwdShow: '../../images/register_eyes_show.png',
    verificationCodeMaxLength: 6,//验证码最大长度
    isFocusInputId: '',
    isPwd: true,
    isConfirmPwd: true,
    pwdValue: '',
    confirmPwdValue: '',
    verificationCodeButtonText: '获取验证码',
    isPressSendVerificationCode: false,
    phoneNum: ''
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // try {
    //   var res = wx.getSystemInfoSync()
    //   console.log(res.model)
    //   console.log(res.pixelRatio)
    //   console.log(res.windowWidth)
    //   console.log(res.windowHeight)
    //   console.log(res.language)
    //   console.log(res.version)
    //   console.log(res.platform)
    // } catch (e) {
    //   // Do something when catch error
    // }
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  formSubmit: function (e) {
    this.setData({ isFocusInputId: '' });
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var _paramObj = e.detail.value;
    let _phonenum = _paramObj.phonenum;
    if (_phonenum == '') {
      _tip.toast('请输入手机号码!');
      return;
    }
    if (!_check.isPhone(_phonenum)) {
      _tip.toast('手机号码格式有误!');
      return;
    }
    let _password = _paramObj.password;

    if (_password == '') {
      _tip.toast('请输入密码!');
      return;
    }

    if (_password.length < 6) {
      _tip.toast('输入的密码不能小于6位!');
      return;
    }

    let _confirmpassword = _paramObj.confirmpassword;
    if (_password != _confirmpassword) {
      _tip.toast('两次密码输入不一致!');
      return;
    }
    if (!this.data.isPressSendVerificationCode) {
      _tip.toast('请先获取验证码!');
      return;
    }
    let _recommendedpersoncode = _paramObj.recommendedpersoncode;
    let _verificationcode = _paramObj.verificationcode;
    if (_verificationcode == '') {
      _tip.toast('请输入验证码!');
      return;
    }
    console.log(_check.isEmail(_phonenum) + "  " + _phonenum + " " + _password + " " + _confirmpassword + " " + _recommendedpersoncode + " " + _verificationcode);
    _swit.switClose('../download/download');
  },
  sendVerificationCode: function () {
    this.setData({ isFocusInputId: '' });
    if ('获取验证码' != this.data.verificationCodeButtonText) {
      return;
    }
    if ('' == this.data.phoneNum) {
      _tip.toast('请输入手机号码!');
      return;
    }

    if (!_check.isPhone(this.data.phoneNum)) {
      _tip.toast('手机号码格式有误!');
      return;
    }
    this.data.isPressSendVerificationCode = true;
    var _count = 180;
    var _this = this;
    var _flag = setInterval(function () {
      if (_count < 0) {
        clearInterval(_flag);
        _this.setData({ verificationCodeButtonText: "获取验证码" });
      } else {
        _this.setData({ verificationCodeButtonText: _count + " s " });
        _count--;
      }

    }, 1000)
  },
  showOrHidePwd: function (e) {
    let currentId = e.target.id;
    console.log(e);
    if ('password' == currentId) {
      this.setData({ isPwd: !this.data.isPwd, pwdValue: this.data.pwdValue });
    } else if ('confirmpassword' == currentId) {
      this.setData({ isConfirmPwd: !this.data.isConfirmPwd, confirmPwdValue: this.data.confirmPwdValue });
    }
  },
  bindKeyInput: function (e) {
    let currentId = e.target.id;
    if ('pwdinput' == currentId) {
      this.data.pwdValue = e.detail.value;
    } else if ('confirmpwdinput' == currentId) {
      this.data.confirmPwdValue = e.detail.value;
    } else if ('phonenum' == currentId) {
      this.data.phoneNum = e.detail.value;
    }
  },
  bindFocusInput: function (e) {
    this.setData({ isFocusInputId: e.target.id });
  },
  onClick: function (e) {
    _swit.swit('../login/login');
  }
})